package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.RecommendVideo;
import com.itheima.domain.mongo.Video;
import com.itheima.service.mongo.VideoService;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class VideoServiceImp implements VideoService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private RedisIDService redisIDService;
    @Override//查询推荐视频分页
    public PageBeanVo findVideoByPage(Long userId, Integer pageNum, Integer pageSize) {
        //创建条件为推荐给用户的id相同.按照缘分值降序，并分页
        Query query=new Query(Criteria.where("userId").is(userId))
                .with(Sort.by(Sort.Order.desc("score")))
                .skip((pageNum-1)*pageSize).limit(pageSize);
        //调用mongo查询满足条件列表
        List<RecommendVideo> recommendVideos = mongoTemplate.find(query, RecommendVideo.class);
        //创建视频集合
        List<Video> videos=new ArrayList<>();
        //判断满足条件的推荐视频列表 非空遍历
        if (CollectionUtil.isNotEmpty(recommendVideos)) {
            for (RecommendVideo recommendVideo : recommendVideos) {
                //根据推荐视频中的视频id查询出视频详情
                Video video = mongoTemplate.findById(recommendVideo.getVideoId(), Video.class);
                //添加到视频集合
                videos.add(video);
            }
        }//调用mongo查询出满足条件的数量
        long count = mongoTemplate.count(query, RecommendVideo.class);
        //创建赋值并返回分页类
        return new PageBeanVo(pageNum,pageSize,count,videos);
    }

    @Override//发布视频
    public void saveVideo(Video video) {
        //使用redis自增器获取视频vid
        Long vid=redisIDService.getNextId(ConstantUtil.VIDEO_ID);
        //赋值并保存
        video.setVid(vid);
        mongoTemplate.save(video);
        //创建推荐视频类 赋值并保存
        RecommendVideo recommendVideo=new RecommendVideo();
        recommendVideo.setDate(System.currentTimeMillis());
        recommendVideo.setScore(99D);
        recommendVideo.setUserId(video.getUserId());
        recommendVideo.setVid(vid);
        recommendVideo.setVideoId(video.getId());
        mongoTemplate.save(recommendVideo);
    }

    @Override//根据id查询视频
    public Video findVideoById(ObjectId id) {
        Video video = mongoTemplate.findById(id, Video.class);
        return video;
    }

    @Override//根据用户id查询视频并分页
    public PageBeanVo findVideoVoByUserIdPage(Long userId, Integer pageNum, Integer pageSize) {
        //创建用户id相同 创建时间降序并分页
        Query query=new Query(Criteria.where("userId").is(userId))
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum-1)*pageSize);
        //调用mongo查询满足条件的视频集合
        List<Video> videos=mongoTemplate.find(query,Video.class);
        //调用mongo查询满足条件的视频数量
        Long counts=mongoTemplate.count(query,Video.class);
        //创建分页类 赋值并返回
        return new PageBeanVo(pageNum,pageSize,counts,videos);
    }
}
